<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\user\controller;

use app\portal\api\PageApi;
use app\portal\controller\PageController;
use app\portal\model\PortalCategoryModel;

use app\user\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminIndexController
 * @package app\user\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class ParentIndexController extends AdminBaseController
{

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $categoriesModel=new UserModel();
        $status = $this->request->param('category', 0, 'intval');
        $categories=$categoriesModel->statusCategory($status);
//        $this->assign('category_tree',$categories);
        $where   = ['user_type'=>2];
        $request = input('request.');

        if (!empty($request['uid'])) {
            $where['id'] = intval($request['uid']);
        }
        $keywordComplex = [];
        if (!empty($request['user_login'])) {
            $keyword = $request['user_login'];
            $keywordComplex['u.user_login']    = ['like', "%$keyword%"];
        }
//        $user_status=empty($_POST['user_status'])?0:1;
//        var_dump($user_status);
        if (empty($request['user_status']) && isset($request['user_status'])) {
            $userstatus = $request['user_status'];
            $userstatus = $request['user_status'];
            if ($userstatus == 0) {
                $this->assign('b','selected');
            }
            $keywordComplex['user_status']    = $userstatus;
        }else if(!empty($request['user_status'])){
            $userstatus = $request['user_status'];
            $userstatus = $request['user_status'];
            if ($userstatus == 1) {
                $this->assign('a', 'selected');
                $keywordComplex['user_status']    = $userstatus;
            }else if($userstatus == 3) {
                $this->assign('f', 'selected');

                unset($keywordComplex['user_status']);
            }

        }



        if (!empty($request['user_status1'])) {
            $keyword = $request['user_status1'];
            $keywordComplex['user_status1']    = $keyword;
            $user_status1=$request['user_status1'];
            if($user_status1==1)
            {
                $this->assign('c','selected');
            }else if($user_status1==2)
            {
                $this->assign('d','selected');

            }else if($user_status1==3)
            {
                $this->assign('e','selected');
                unset($keywordComplex['user_status1']);
            }
        }
        if (!empty($request['user_nickname'])) {
            $keyword = $request['user_nickname'];
            $keywordComplex['u.user_nickname'] = ['like', "%$keyword%"];

        }
        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];
            $keywordComplex['u.mobile|u.user_email|u.realname|u.user_nickname|u.user_login'] = ['like', "%".trim($keyword)."%"];

        }
        $startTime = empty($request['start_time']) ? 0 : strtotime($request['start_time']);
        $endTime   = empty($request['end_time']) ? 0 : strtotime($request['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $keywordComplex['u.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $keywordComplex['u.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $keywordComplex['u.create_time'] = ['<= time', $endTime];
            }
        }

        $usersQuery = Db::name('user');
        $list = $usersQuery->alias('u')->field('u.*,thu.third_party')
            ->join('third_party_user thu', 'thu.user_id=u.id',$type='left')
            ->where($where)->where($keywordComplex)->order("create_time DESC")->paginate(10);
//        $tag='field:user_login';
//        $content=sp_sql_posts_paged($tag);
//        $list = $usersQuery->where($where)->whereOr($keywordComplex)->order("create_time DESC")->paginate(3);
//
//

        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }


    public function edit($id)
    {
        $user_parent=new UserModel();
        $data=$user_parent->where('id',$id)->find();
//        echo '<pre>';
//        var_dump($data);
        $this->assign('vo',$data);
        return $this->fetch();

    }
    public function editPost()
    {
        if($this->request->isPost())
        {
            $post=$this->request->post();
            $request =input("request.");
//            var_dump($request);
            $id=input('id');
            $user=new UserModel();
            $result=$user->where('id',$id)->update($post);
            $this->success('修改成功',url('parentIndex/index'));
        }
    }
    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
            if ($result) {
                $this->success("会员拉黑成功！", url("parentIndex/index"));
            } else {
                $this->error('会员拉黑失败,会员不存在,或者是管理员！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
            $this->success("会员启用成功！", '');
        } else {
            $this->error('数据传入失败！');
        }
    }


    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 1) {
            $this->error("最高管理员不能删除！");
        }

        if (Db::name('user')->delete($id) !== false) {
            Db::name("RoleUser")->where(["user_id" => $id])->delete();
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

}
